package com.secneo.appshare.remind;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.secneo.appshare.R;
import com.secneo.appshare.activities.UserAppActivity;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.threads.RemindCountThread;

public class RemindService extends Service {

	private NotificationManager myNotiManager ;	//状态栏通知
	
	private final Timer timer = new Timer();

	private final int TIMERSCHEDULER = 1000 * 60 * 10; // 多长时间检查一次未读提醒10分钟

	private RemindCountThread thread = null ;
	
	@Override
	public IBinder onBind(Intent intent) {
		
		return null;
	}

	@Override
	public void onCreate() {
		
		myNotiManager=  
		      (NotificationManager)getSystemService(NOTIFICATION_SERVICE) ;
		timer.schedule(timerTask, 0, TIMERSCHEDULER);
	}

	@Override
	public void onStart(Intent intent, int startId) {
		
	} 
	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1: {
				thread = new RemindCountThread(RemindService.this, handler) ;
				thread.start() ;
			}
				break;
			case 2:	//OK	has new remind
				 /* 创建新的Intent，作为单击Notification留言条时，  
			     * 会运行的Activity */   
			    Intent notifyIntent=new Intent(RemindService.this,UserAppActivity.class);    
			    
			    notifyIntent.putExtra(Constants.NOTES_INTENT_ACTION, 
			    		Constants.NOTES_INTENT_ACTION_TAG) ;
			    
			    //notifyIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK); 
			    notifyIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			    /* 创建PendingIntent作为设置递延运行的Activity */   
			    PendingIntent appIntent=PendingIntent.getActivity(RemindService.this,  
			                            0,notifyIntent,0); 
			    
			 
			    /* 创建Notication，并设置相关参数 */   
			    Notification myNoti=new Notification();  
			    /* 设置statusbar显示的icon */  
			    myNoti.icon=R.drawable.icon;  
			    /* 设置statusbar显示的文字信息 */  
			    String new_msg = getResources().getString(R.string.new_msg) ;
			    myNoti.tickerText= new_msg ;  
			    /* 设置notification发生时同时发出默认声音 */  
			    myNoti.defaults=Notification.DEFAULT_VIBRATE;  
			    /* 设置Notification留言条的参数 */  
			    Bundle data = msg.getData() ;
			    
			    int new_fans_count = data.getInt(
			    		Constants.NEW_FANS_COUNT_TAG) ;	//新粉丝
			    int new_activitys_count = data.getInt(
			    		Constants.NEW_Activity_COUNT_TAG) ;	//新活动
			    int new_msg_count = data.getInt(
			    		Constants.NEW_MSG_COUNT_TAG) ;	//新私信
			    int new_honor_count = data.getInt(
			    		Constants.NEW_HONOR_COUNT_TAG) ;	//新 勋章
			    
			    StringBuilder sb = new StringBuilder() ;
			    if(new_fans_count != 0){
			    	sb.append(new_fans_count) ;
			    	String new_fans = getResources().getString(R.string.new_fllower) ;
			    	sb.append(new_fans) ;
			    	sb.append(",") ;
			    }
			    if(new_activitys_count != 0){
			    	sb.append(new_activitys_count) ;
			    	String new_activitys = getResources().getString(R.string.new_active) ;
			    	sb.append(new_activitys) ;
			    	sb.append(",") ;
			    }
			    if(new_msg_count != 0){
			    	sb.append(new_msg_count) ;
			    	String new_msg1 = getResources().getString(R.string.new_private_msg) ;
			    	sb.append(new_msg1) ;
			    }
			    if(new_msg_count != 0){
			    	sb.append(new_msg_count) ;
			    	String new_msg1 = getResources().getString(R.string.new_private_msg) ;
			    	sb.append(new_msg1) ;
			    }
			    
			    if(new_honor_count != 0){
			    	String new_msg1 = getResources().getString(R.string.new_honor) ;
			    	sb.append(new_msg1) ;
			    }
			    String str = getResources().getString(R.string.remind_prompt) ;
			    myNoti.setLatestEventInfo(RemindService.this,str,
			                              sb.toString(),appIntent);  
			    /* 送出Notification */  
			    myNotiManager.notify(R.string.app_name,myNoti);
				break ;
			case 3:	//faild
				break ;
			default:
				break;
			}
		}
	};

	
	
	private TimerTask timerTask = new TimerTask() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			Message message = new Message();
			message.what = 1;
			handler.sendMessage(message);
		}
	};
}
